<?php
// 采集公共文件

//定义主机

define('LOGIN_HOST','http://175.102.5.178:8080');
//设置cookie保存路径
define('COOKIE_PATH',dirname(__FILE__).'/cookie.txt');

if(file_exists(COOKIE_PATH)){
    unlink(COOKIE_PATH);
}

//登录
function LoginPost($url, $cookie, $post) {
    $curl = curl_init();//初始化curl模块
    curl_setopt($curl, CURLOPT_URL, $url);//登录提交的地址
    curl_setopt($curl, CURLOPT_HEADER, 0);//是否显示头信息
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);//是否自动显示返回的信息

    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); //设置Cookie信息保存在指定的文件中
    curl_setopt($curl, CURLOPT_POST, 1);//post方式提交
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));//要提交的信息
    curl_exec($curl);//执行cURL
    curl_close($curl);//关闭cURL资源，并且释放系统资源
}

//登录成功后获取数据
function GetData($url, $cookie) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //读取cookie
    $rs = curl_exec($ch); //执行cURL抓取页面内容
    curl_close($ch);
    return $rs;
}

//登录成功后获取数据
function GetPost($url,$cookie,$param=null) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);//post方式提交
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));//要提交的信息
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //读取cookie
    $rs = curl_exec($ch); //执行cURL抓取页面内容
    curl_close($ch);

    return $rs;
}

//开始登录
$post = array (
    'j_username' => '008001011',
    'j_password' => '620500'
);
//登录地址
$login_url = LOGIN_HOST."/trader/j_spring_security_check";
//模拟登录
LoginPost($login_url, COOKIE_PATH, $post);